package com.nkl.admin.action;

import com.nkl.admin.manager.AdminManager;
import com.nkl.common.action.BaseAction;
import com.nkl.common.util.DateUtil;
import com.nkl.common.util.Param;
import com.nkl.page.domain.*;

import java.util.Date;
import java.util.List;

//后台管理
public class AdminAction  extends BaseAction {

	private static final long serialVersionUID = 1L;
	AdminManager adminManager = new AdminManager();

	//抓取页面参数
	Infos paramsInfos;
	Comment paramsComment;
	CommentReply paramsCommentReply;
	Note paramsNote;
	User paramsUser;
	UserNo paramsUserNo;
	String tip;
	
	/**
	 * @Title: saveAdmin
	 * @Description: 保存修改个人信息
	 * @return String
	 */
	public String saveAdmin(){
		try {
			//验证用户会话是否失效
			if (!validateAdmin()) {
				return "loginTip";
			}
			 //保存修改个人信息
			adminManager.updateUser(paramsUser);
			//更新session
			User admin = (User)Param.getSession("admin");
			if (admin!=null) {
				admin.setUser_no(paramsUser.getUser_no());
				admin.setNick_name(paramsUser.getNick_name());
				admin.setReal_name(paramsUser.getReal_name());
				admin.setUser_sex(paramsUser.getUser_sex());
				Param.setSession("admin", admin);
			}
			
		} catch (Exception e) {
			setErrorTip("编辑异常", "modifyInfo.jsp");
		}
		setSuccessTip("编辑成功", "modifyInfo.jsp");
		return "infoTip";
	}
	
	/**
	 * @Title: saveAdminPass
	 * @Description: 保存修改个人密码
	 * @return String
	 */
	public String saveAdminPass(){
		try {
			//验证用户会话是否失效
			if (!validateAdmin()) {
				return "loginTip";
			}
			 //保存修改个人密码
			//paramsUser.setUser_pass(Md5.makeMd5(paramsUser.getUser_pass()));
			adminManager.updateUser(paramsUser);
			//更新session
			User admin = (User)Param.getSession("admin");
			if (admin!=null) {
				admin.setUser_pass(paramsUser.getUser_pass());
				Param.setSession("admin", admin);
			}
			
		} catch (Exception e) {
			setErrorTip("修改异常", "modifyPwd.jsp");
		}
		setSuccessTip("修改成功", "modifyPwd.jsp");
		return "infoTip";
	}
	
	/**
	 * @Title: listUserNos
	 * @Description: 查询学号
	 * @return String
	 */
	public String listUserNos(){
		try {
			//查询用户
			if (paramsUserNo==null) {
				paramsUserNo = new UserNo();
			}
			setPagination(paramsUserNo);
			int[] sum={0};
			List<UserNo> userNos = adminManager.listUserNos(paramsUserNo,sum); 
			
			Param.setAttribute("userNos", userNos);
			setTotalCount(sum[0]);
			
		} catch (Exception e) {
			setErrorTip("查询学号异常", "main.jsp");
			return "infoTip";
		}
		
		return "userNoShow";
	}
	
	/**
	 * @Title: addUserNo
	 * @Description: 添加学号
	 * @return String
	 */
	public String addUserNo(){
		try {
			 //添加学号
			adminManager.addUserNo(paramsUserNo);
			
		} catch (Exception e) {
			setErrorTip("添加学号异常", "Admin_listUserNos.action");
		}
		setSuccessTip("添加成功", "Admin_listUserNos.action");
		return "infoTip";
	}
	
	/**
	 * @Title: delUserNos
	 * @Description: 删除学号
	 * @return String
	 */
	public String delUserNos(){
		try {
			 //删除学号
			adminManager.delUserNos(paramsUserNo);
			
		} catch (Exception e) {
			setErrorTip("删除学号异常", "Admin_listUserNos.action");
		}
		setSuccessTip("删除学号成功", "Admin_listUserNos.action");
		return "infoTip";
	}
	
	/**
	 * @Title: listUsers
	 * @Description: 查询用户
	 * @return String
	 */
	public String listUsers(){
		try {
			//查询用户
			if (paramsUser==null) {
				paramsUser = new User();
			}
			paramsUser.setUser_type(1);
			setPagination(paramsUser);
			int[] sum={0};
			List<User> users = adminManager.listUsers(paramsUser,sum); 
			
			Param.setAttribute("users", users);
			setTotalCount(sum[0]);
			
		} catch (Exception e) {
			setErrorTip("查询用户异常", "main.jsp");
			return "infoTip";
		}
		
		return "userShow";
	}
	
	/**
	 * @Title: addUserShow
	 * @Description: 显示添加用户页面
	 * @return String
	 */
	public String addUserShow(){
		return "userEdit";
	}
	
	/**
	 * @Title: addUser
	 * @Description: 添加用户
	 * @return String
	 */
	public String addUser(){
		try {
			 //添加用户
			paramsUser.setUser_type(1);
			paramsUser.setReg_date(DateUtil.dateToDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
			adminManager.addUser(paramsUser);
			
		} catch (Exception e) {
			setErrorTip("添加用户异常", "Admin_listUsers.action");
		}
		setSuccessTip("添加成功", "Admin_listUsers.action");
		return "infoTip";
	}
	
	 
	/**
	 * @Title: editUser
	 * @Description: 编辑用户
	 * @return String
	 */
	public String editUser(){
		try {
			 //得到用户
			User user = adminManager.getUser(paramsUser);
			Param.setAttribute("user", user);
			
		} catch (Exception e) {
			setErrorTip("查询用户异常", "Admin_listUsers.action");
			return "infoTip";
		}
		
		return "userEdit";
	}
	
	/**
	 * @Title: saveUser
	 * @Description: 保存编辑用户
	 * @return String
	 */
	public String saveUser(){
		try {
			 //保存编辑用户
			adminManager.updateUser(paramsUser);
			
		} catch (Exception e) {
			tip="编辑失败";
			Param.setAttribute("user", paramsUser);
			return "userEdit";
		}
		setSuccessTip("编辑成功", "Admin_listUsers.action");
		return "infoTip";
	}
	
	/**
	 * @Title: delUsers
	 * @Description: 删除用户
	 * @return String
	 */
	public String delUsers(){
		try {
			 //删除用户
			adminManager.delUsers(paramsUser);
			
		} catch (Exception e) {
			setErrorTip("删除用户异常", "Admin_listUsers.action");
		}
		setSuccessTip("删除用户成功", "Admin_listUsers.action");
		return "infoTip";
	}
	
	/**
	 * @Title: listNotes
	 * @Description: 查询公告
	 * @return String
	 */
	public String listNotes(){
		try {
			//查询公告
			if (paramsNote==null) {
				paramsNote = new Note();
			}
			setPagination(paramsNote);
			int[] sum={0};
			List<Note> notes = adminManager.listNotes(paramsNote,sum); 
			
			Param.setAttribute("notes", notes);
			setTotalCount(sum[0]);
			
		} catch (Exception e) {
			setErrorTip("查询公告异常", "main.jsp");
			return "infoTip";
		}
		
		return "noteShow";
	}
	
	/**
	 * @Title: addNoteShow
	 * @Description: 显示添加公告页面
	 * @return String
	 */
	public String addNoteShow(){
		return "noteEdit";
	}
	
	/**
	 * @Title: addNote
	 * @Description: 添加公告
	 * @return String
	 */
	public String addNote(){
		try {
			 //添加公告
			paramsNote.setNote_time(DateUtil.dateToDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
			adminManager.addNote(paramsNote);
			
		} catch (Exception e) {
			setErrorTip("添加公告异常", "Admin_listNotes.action");
		}
		setSuccessTip("添加成功", "Admin_listNotes.action");
		return "infoTip";
	}
	
	 
	/**
	 * @Title: editNote
	 * @Description: 编辑公告
	 * @return String
	 */
	public String editNote(){
		try {
			 //得到公告
			Note note = adminManager.getNote(paramsNote);
			Param.setAttribute("note", note);
			
		} catch (Exception e) {
			setErrorTip("查询公告异常", "Admin_listNotes.action");
			return "infoTip";
		}
		
		return "noteEdit";
	}
	
	/**
	 * @Title: saveNote
	 * @Description: 保存编辑公告
	 * @return String
	 */
	public String saveNote(){
		try {
			 //保存编辑公告
			adminManager.updateNote(paramsNote);
			
		} catch (Exception e) {
			tip="编辑失败";
			Param.setAttribute("note", paramsNote);
			return "noteEdit";
		}
		setSuccessTip("编辑成功", "Admin_listNotes.action");
		return "infoTip";
	}
	
	/**
	 * @Title: delNotes
	 * @Description: 删除公告
	 * @return String
	 */
	public String delNotes(){
		try {
			 //删除公告
			adminManager.delNotes(paramsNote);
			
		} catch (Exception e) {
			setErrorTip("删除公告异常", "Admin_listNotes.action");
		}
		setSuccessTip("删除公告成功", "Admin_listNotes.action");
		return "infoTip";
	}
	
	/**
	 * @Title: listInfoss
	 * @Description: 查询资讯
	 * @return String
	 */
	public String listInfoss(){
		try {
			//查询资讯
			if (paramsInfos==null) {
				paramsInfos = new Infos();
			}
			setPagination(paramsInfos);
			int[] sum={0};
			List<Infos> infoss = adminManager.listInfoss(paramsInfos,sum); 
			
			Param.setAttribute("info_type", paramsInfos.getInfo_type());
			Param.setAttribute("info_typeDesc", paramsInfos.getInfo_typeDesc());
			Param.setAttribute("infoss", infoss);
			setTotalCount(sum[0]);
			
		} catch (Exception e) {
			setErrorTip("查询资讯异常", "main.jsp");
			return "infoTip";
		}
		
		return "infosShow";
	}
	
	/**
	 * @Title: addInfosShow
	 * @Description: 显示添加资讯页面
	 * @return String
	 */
	public String addInfosShow(){
		Param.setAttribute("info_type", paramsInfos.getInfo_type());
		Param.setAttribute("info_typeDesc", paramsInfos.getInfo_typeDesc());
		return "infosEdit";
	}
	
	/**
	 * @Title: addInfos
	 * @Description: 添加资讯
	 * @return String
	 */
	public String addInfos(){
		try {
			 //添加资讯
			paramsInfos.setInfo_time(DateUtil.dateToDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
			adminManager.addInfos(paramsInfos);
			
		} catch (Exception e) {
			setErrorTip("添加"+paramsInfos.getInfo_typeDesc()+"异常", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
		}
		setSuccessTip("添加成功", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
		return "infoTip";
	}
	
	 
	/**
	 * @Title: editInfos
	 * @Description: 编辑资讯
	 * @return String
	 */
	public String editInfos(){
		try {
			 //得到资讯
			Infos infos = adminManager.getInfos(paramsInfos);
			Param.setAttribute("info_type", paramsInfos.getInfo_type());
			Param.setAttribute("info_typeDesc", paramsInfos.getInfo_typeDesc());
			Param.setAttribute("infos", infos);
			
		} catch (Exception e) {
			setErrorTip("查询"+paramsInfos.getInfo_typeDesc()+"异常", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
			return "infoTip";
		}
		
		return "infosEdit";
	}
	
	/**
	 * @Title: saveInfos
	 * @Description: 保存编辑资讯
	 * @return String
	 */
	public String saveInfos(){
		try {
			 //保存编辑资讯
			adminManager.updateInfos(paramsInfos);
			
		} catch (Exception e) {
			tip="编辑失败";
			Param.setAttribute("info_type", paramsInfos.getInfo_type());
			Param.setAttribute("info_typeDesc", paramsInfos.getInfo_typeDesc());
			Param.setAttribute("infos", paramsInfos);
			return "infosEdit";
		}
		setSuccessTip("编辑成功", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
		return "infoTip";
	}
	
	/**
	 * @Title: delInfoss
	 * @Description: 删除资讯
	 * @return String
	 */
	public String delInfoss(){
		try {
			 //删除资讯
			adminManager.delInfoss(paramsInfos);
			
		} catch (Exception e) {
			setErrorTip("删除异常", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
		}
		setSuccessTip("删除成功", "Admin_listInfoss.action?paramsInfos.info_type="+paramsInfos.getInfo_type());
		return "infoTip";
	}
	
	/**
	 * @Title: listComments
	 * @Description: 查询新闻评论
	 * @return String
	 */
	public String listComments(){
		try {
			//查询新闻评论
			if (paramsComment ==null) {
				paramsComment = new Comment();
			}
			setPagination(paramsComment);
			int[] sum={0};
			List<Comment> comments = adminManager.listComments(paramsComment,sum);
			
			Param.setAttribute("comments", comments);
			setTotalCount(sum[0]);
			
		} catch (Exception e) {
			setErrorTip("查询新闻评论异常", "main.jsp");
			return "infoTip";
		}
		
		return "commentShow";
	}
	
	/**
	 * @Title: delComments
	 * @Description: 删除新闻评论
	 * @return String
	 */
	public String delComments(){
		try {
			 //删除新闻评论
			adminManager.delComments(paramsComment);
			
		} catch (Exception e) {
			setErrorTip("删除新闻评论异常", "Admin_listComments.action");
		}
		setSuccessTip("删除新闻评论成功", "Admin_listComments.action");
		return "infoTip";
	}
	
	
	private boolean validateAdmin(){
		User admin = (User)Param.getSession("admin");
		if (admin!=null) {
			return true;
		}else {
			return false;
		}
	}
	
	private void setErrorTip(String tip,String url){
		Param.setAttribute("tipType", "error");
		Param.setAttribute("tip", tip);
		Param.setAttribute("url1", url);
		Param.setAttribute("value1", "确 定");
	}
	
	private void setSuccessTip(String tip,String url){
		Param.setAttribute("tipType", "success");
		Param.setAttribute("tip", tip);
		Param.setAttribute("url1", url);
		Param.setAttribute("value1", "确 定");
	}
	
	
	public Infos getParamsInfos() {
		return paramsInfos;
	}

	public void setParamsInfos(Infos paramsInfos) {
		this.paramsInfos = paramsInfos;
	}

	public Comment getParamsComment() {
		return paramsComment;
	}

	public void setParamsComment(Comment paramsComment) {
		this.paramsComment = paramsComment;
	}

	public Note getParamsNote() {
		return paramsNote;
	}

	public void setParamsNote(Note paramsNote) {
		this.paramsNote = paramsNote;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public CommentReply getParamsCommentReply() {
		return paramsCommentReply;
	}

	public void setParamsCommentReply(CommentReply paramsCommentReply) {
		this.paramsCommentReply = paramsCommentReply;
	}

	public User getParamsUser() {
		return paramsUser;
	}

	public void setParamsUser(User paramsUser) {
		this.paramsUser = paramsUser;
	}

	public UserNo getParamsUserNo() {
		return paramsUserNo;
	}

	public void setParamsUserNo(UserNo paramsUserNo) {
		this.paramsUserNo = paramsUserNo;
	}
}
